﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using PciDeviceLib;

namespace CPCIDemo
{
    public partial class Form1 : Form
    {
        PCI_Control pCI_Control = new PCI_Control();
        List<String> DeviceList = new List<string>();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

         UInt32   DeviceList = pCI_Control.DeviceInit(0XC514,0);//(0XC514,0X100);
            if (DeviceList != null)
            {
                foreach (String dev in PCI_Control.pciDeviceList)
                {
                    板卡列表.Items.Add(dev.ToString());
                }
            }
            else
            {
                板卡列表.Items.Add("null");
            }
   

        }
        private void 打开设备_Click(object sender, EventArgs e)
        {
            try
            {

                if (pCI_Control.DeviceOpen(板卡列表.SelectedIndex)==0)
                {
                    打开设备.Enabled = false;
                    板卡列表.Enabled = false;
                }
            }
            catch
            { }
        }

        private void 关闭设备_Click(object sender, EventArgs e)
        {
            if (pCI_Control.DeviceClose(板卡列表.SelectedIndex))
            {
                打开设备.Enabled = true;
                板卡列表.Enabled = true;
            }
        }

        private void R1_Click(object sender, EventArgs e)
        {
         //   UInt32 ffset = (UInt32)Convert.ToInt32("2222", 16);
            UInt32 Rdata = 0;
            UInt32 U32 = (UInt32) Convert.ToInt32(addressR1.Text.ToString(), 16);
            pCI_Control.ReadAddr32(板卡列表.SelectedIndex, U32, ref Rdata);
          
            dataR1.Text = Rdata.ToString("X8");

            string sData = DeviceList[板卡列表.SelectedIndex]+"读到的数据: " + Rdata.ToString("X8");
            LogtxtData.Text += sData + Environment.NewLine;
        }

        private void 置顶显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (置顶显示ToolStripMenuItem.Checked == false)
            {
                TopMost = true;
                置顶显示ToolStripMenuItem.Checked = true;
            }
            else
            {
                TopMost = false;
                置顶显示ToolStripMenuItem.Checked = false;
            }
          
        }

        private void W1_Click(object sender, EventArgs e)
        {
            
            UInt32 U32address = (UInt32)Convert.ToInt32(addressW1.Text.ToString(), 16);
            UInt32 U32data = (UInt32)Convert.ToInt32(dataW1.Text.ToString(), 16);

            pCI_Control.WriteAddr32(板卡列表.SelectedIndex, U32address, U32data);

            string sData = DeviceList[板卡列表.SelectedIndex] + "写入的数据: " + U32data.ToString("X8");
            LogtxtData.Text += sData + Environment.NewLine;
        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {
            bool bo1 = checkBox7.Checked;
        }

        private void DMA_Read_Click(object sender, EventArgs e)
        {


            UInt32 U32address = (UInt32)Convert.ToInt32(DMA_faddress.Text.ToString(), 16);
            UInt32 U32data = (UInt32)Convert.ToInt32(DMA_sum.Text.ToString(), 16);
            UInt32[] Rdata = new UInt32[U32data/4];
            pCI_Control.ReadAddrDMA(板卡列表.SelectedIndex, U32address, U32data, ref Rdata);
            string disdata = "";
            string disdata2 = "";
            int m = 0;
         
            foreach (UInt32 item in Rdata)
            {
                m++;
                disdata += (item).ToString("X8") + " ";
                disdata2 += m.ToString("0000") + ":" + (item).ToString("X8") + Environment.NewLine;
            }

            LogtxtData.Text = disdata2;
            DMAReadData.Text = disdata;
        }

        private void button15_Click(object sender, EventArgs e)
        {
            return;
            //UInt32[] Rdata = new UInt32[4];
            //Rdata[0] = 0;
            //Rdata[1] = 0;
            //Rdata[2] = 0;
            //Rdata[3] = 0;
            //pCI_Control.WriteAddrDMA(板卡列表.SelectedIndex, "140", "10",  Rdata);
        }
    }
}
